<?php

namespace app\controller\admin;

use app\bll\ComponentWidget as BllComponentWidget;
use app\bll\Plugin;
use app\bll\Site as BllSite;
use app\bll\UserWidget as BllUserWidget;
use core\controller\AdminBase;
use core\lib\schema\Schema;
use core\lib\tool\File;
use Exception;
use ReflectionClass;
use support\Db;
use support\Request;
use support\View;

class Index  extends AdminBase
{

    public function index(Request $request)
    {

        $op = request()->get('op');
        switch ($op) {
            case 'init':
                break;
                break;
            case 'addWidget':
                return  $this->addWidget();
                break;
            case 'editWidget':
                return  $this->editWidget();
                break;
            case 'delWidget':
                return  $this->delWidget();
                break;
        }
        //获取挂件
        $bllComponentWidget = new BllComponentWidget();
        $myallwidgets = $bllComponentWidget->selectByIdents();


        //获取本人已布局挂件
        $bllUserWidget   = new BllUserWidget();
        $mylayoutwidgets = $bllUserWidget->selectByUid(request()->admininfo['id']);
        foreach ($mylayoutwidgets as $key => $widget) {
            $mylayoutwidgets[$key]['content'] = $this->showWidget($widget['widget'], $widget['content']);
        }

        View::assign('myallwidgets', $myallwidgets);
        View::assign('mylayoutwidgets', json_encode($mylayoutwidgets, JSON_UNESCAPED_UNICODE));
        return $this->view();
    }



    /**************************************** */


    /**
     * 显示挂件
     *
     * @return mixed
     */
    private function showWidget($name, $content)
    {
        $bllComponentWidget = new BllComponentWidget();
        $widget             = $bllComponentWidget->findByIdent($name);

        $widget = ($widget['type'] == 1 ? 'admin' : 'uc') . '\\' . $name;
        $html = '';
        $reflectionClass = new ReflectionClass(
            'component\\widget\\'
                . $widget
                . '\\controller\\Index'
        );

        $method = $reflectionClass->getMethod('index');
        if ($method->isPublic()) {
            $instance = $reflectionClass->newInstance();
            $html =  $method->invoke($instance, $content);
        }
        return $html;
    }


    private function addWidget()
    {
        try {
            $data = request()->post();
            $bllUserWidget = new BllUserWidget();
            $userwidgetid  = $bllUserWidget->add( request()->admininfo['id'], $data['widget'], $data);
            $userwidget    = $bllUserWidget->findByUid($userwidgetid, request()->admininfo['id']);

            $widget = ($userwidget['widgettype'] == 1 ? 'admin' : 'uc') . '\\' . $userwidget['widget'];
            $html = '';
            $reflectionClass = new ReflectionClass(
                'component\\widget\\'
                    . $widget
                    . '\\controller\\Index'
            );

            $method = $reflectionClass->getMethod('index');
            if ($method->isPublic()) {
                $instance = $reflectionClass->newInstance();
                $html =  $method->invoke($instance, $userwidget['content']);
            }
            return $this->success(['widget' => $html]);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage() . $ex->getLine());
        }
    }


    private function editWidget()
    {
        try {
            $bllUserWidget = new BllUserWidget();
            $data          = request()->post('items');
            foreach ($data as $key => $d) {
                $bllUserWidget->edit(request()->admininfo['id'], $d['areaid'], $d);
            }
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    public function delWidget()
    {
        try {
            $bllUserWidget = new BllUserWidget();
            $areaid        = request()->post('id');
            $bllUserWidget->delete(request()->admininfo['id'], $areaid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
